<?php
/**
 * Trida slouzi pro zpracovani obrazku pri importu dat z API data publisheru.
 *
 * @author David Dusek
 */
class PictureImport {
    /**
     * Obsahuje informace o obrazku ziskane rozparsovanim xml z API
     * @var array
     */
    private $data = array();
    
    /**
     * Obsahuje xml retezec ziskany z API     
     * @var string
     */
    private $XMLString = null;
    
    /**
     * Instanece tridy DataRequest pr zasilani pozadavku na API.
     * @var DataRequest 
     */
    private $dataRequest = null;
     
    /**
     * Pripojeni k databazi.
     * @var NConnection db
     */
    private $db = null;
    
    /**
     * Vychozi konstruktor inicializuje atribut dataReguest.
     * @param Nconnection $db 
     */
    public function __construct($db) {
        $this->dataRequest = new DataRequest();
        $this->db = $db;
    }      
    
    /**
     * Import jednoho obrazku do databaze a do souboru.
     * @param int $pictureId
     * @param int $estateId
     * @param string $description
     * @param int $order 
     */
    public function importImage($pictureId, $estateId, $description, $order) {                        
        if ($this->getImageInformation($pictureId)) {                         
            $this->data["description"] = $description;
            $this->data["pic_order"] = $order;            
            $this->data["estate"] = $estateId;
            
            $table = new PictureModel($this->db);
            if ($table->get($pictureId) === FALSE) {
                $table->insert($this->data);
            } else {                 
                $table->where("id", $pictureId)->update($this->data);
            }
        }
    }
    
    /**
     * Ziskani informaci o obrazku z API. Po provedeni je xml odpoved z API
     * ulozena v atributu XMLString a rozparsovana data v atributu data.
     * @param int $id id obrazku
     * @return bool true pokud je operace uspesna jinak false. 
     */
    private function getImageInformation($id) {
        $parametrs = array(
            "Login"      => DataRequest::$login,
            "Password"   => DataRequest::$password,
            "FileDataId" => $id
        );
        
        // odeslani pozadavku do na API
        $this->XMLString = $this->dataRequest->sendRequest($parametrs, 
                DataRequest::$pathFileData);        
        // rozparsovani XML s informacemi o obrazku        
        if ($this->parseImageInformation()) {
            return true;
        }
        return false;
    }
    
    /**
     * Rozparsovani informaci o obrazku a ulozeni do atributu data.
     * @return bool true pri uspechu jinak false 
     */
    private function parseImageInformation() {
        if ($this->XMLString === null) {
            return false;
        }
        
        $document = simplexml_load_string($this->XMLString);
        if ($document) {
            $this->data = array(
                "id"            => (int) $document->Data->Id,
                "filename"      => (string) $document->Data->FileName,
                "content_type"  => (string) $document->Data->ContentType,
                "data"          => (string) $document->Data->DataBase64,                
            );                        
        } else {
            return false;
        }
        return true;
    }
    
    /**
     * Funkce pro testovani pozadavku.
     * @param int $id id obrazku
     * @return String 
     */
    public function getXMLData($id) {
        $this->getImageInformation($id);
        return $this->XMLString;
    }
}
